package com.atguigu.gmall.item.config;


import com.atguigu.gmall.common.config.thread.AppThreadPoolConfig;
import com.atguigu.gmall.common.constant.RedisConstant;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import(AppThreadPoolConfig.class)
@Configuration
public class AppBloomFilterConfig {

    @Autowired
    RedissonClient redissonClient;

    /**
     * 商品id的布隆过滤器
     * @return
     */
    @Bean
    public RBloomFilter<Object> skuIdBloom(){
        //1、获取redis中的布隆过滤器
        RBloomFilter<Object> filter = redissonClient.getBloomFilter(RedisConstant.SKUIDS_BLOOM);

       if(filter.isExists() ){ //判断这个布隆是否存在
            //如果以前有布隆就不用初始化
           System.out.println("远程布隆存在...啥都不干");
       }else {
           //2、初始化布隆；这个一调用，远程数据都没有了
           filter.tryInit(1000000,0.000001);
           System.out.println("远程布隆不存在...已经帮你初始化一个");
       }

        return  filter;
    }
}
